package com.smec.moblie.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnItemClickListener;
import com.smec.moblie.R;
import com.smec.moblie.crash.ExitAppUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by sunyubo on 16/3/10.
 */
public class SetUpActivity extends BaseActivity {
    /**
     * 返回按钮
     **/
    @BindView(R.id.imageview_back)
    ImageView imageview_back;
    @BindView(R.id.textview_getDevice)
    /** 获取设备领用人信息 **/
            TextView textview_getDevice;
    /**
     * 修改手势密码
     **/
    @BindView(R.id.textView_changePatter)
    TextView textView_changePatter;
    /**
     * 清除缓存
     **/
    @BindView(R.id.textView_clearData)
    TextView textView_clearData;
    /**
     * 登出按钮
     **/
    @BindView(R.id.button_logout)
    Button button_logout;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;

    /****
     * 存储账号密码SharedPreferences
     ****/
    private SharedPreferences mySharedPreferences;

    /****
     * 是否保存密码
     ****/
    private Boolean isSetPassWord;

    @Override
    protected void setContentView() {
        setContentView(R.layout.remes_act_setup);
    }

    @Override
    protected void initView() {
        ExitAppUtils.getInstance().addActivity(this);

//        PublicControl.setMargins(rlTitle, 0, getStatusBarHeight(SetUpActivity.this), 0, 0);

        ButterKnife.bind(this);
        imageview_back.setOnClickListener(this);
        textView_changePatter.setOnClickListener(this);
        textView_clearData.setOnClickListener(this);
        button_logout.setOnClickListener(this);
        textview_getDevice.setOnClickListener(this);
        mySharedPreferences = getSharedPreferences(
                "ElevatorSharedPreferences", LoginActivity.MODE_PRIVATE);
        isSetPassWord = mySharedPreferences.getBoolean("isSetPassWord", false);
        if (!isSetPassWord) {
            textView_changePatter.setText("设置手势密码");
        }
    }

    @Override
    protected void getData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageview_back:
                SetUpActivity.this.finish();
                break;
            case R.id.textview_getDevice:
                Intent deviceIntent = new Intent(this, DeviceInfoActivity.class);
                startActivity(deviceIntent);
                break;
            case R.id.textView_changePatter:
                Intent intent;

                if (isSetPassWord) {
                    intent = new Intent(SetUpActivity.this, ConfirmPatternActivity.class);
                } else {
                    intent = new Intent(SetUpActivity.this, SetPatternActivity.class);
                }
                intent.putExtra("isChangePatter", true);
                startActivity(intent);
                break;
            case R.id.textView_clearData:
                try {
                    Intent intent2 = new Intent(Intent.ACTION_VIEW);
                    intent2.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    Uri uri = Uri.fromParts("package", "com.smec.moblie", null);
                    intent2.setData(uri);
                    startActivity(intent2);
                } catch (Exception e) {
                    Toast.makeText(this, "当前系统不支持此功能", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.button_logout:
                new AlertView("提示", "确定要退出当前账号？", "取消", new String[]{"确定"}, null, this, AlertView.Style.Alert, new OnItemClickListener() {
                    @Override
                    public void onItemClick(Object o, int position) {
                        if (position == 0) {
                            SharedPreferences.Editor editor = mySharedPreferences
                                    .edit();
                            editor.putBoolean("isSetPassWord", false);
                            // 提交当前数据
                            editor.commit();
                            Intent intent = new Intent(SetUpActivity.this, LoginActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);//清除所有除了最后跳转到的activity
                            startActivity(intent);
                        }
                    }
                }).show();
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

}
